package com.example.play.service;

import com.alibaba.fastjson.JSONArray;
import com.api.core.result.JsonResponse;
import com.api.core.result.ResultCodeEnum;
import com.example.play.bean.Config;
import com.example.play.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author wsl
 * @version 1.0
 * @date 2020/5/18
 * @description 这是 文件服务
 */


@Service
public class FileService {

    @Autowired
    private FileUtils fileUtils;
    @Autowired
    private Config config;

    /**
     * 不支持的-文件类型
     */
    private static final List<String> falseType = new ArrayList<> ();


    public JsonResponse uploadFile(MultipartFile file) {
        if (checkType (getType (file))) {
            String path = fileUtils.saveFile (Paths.get (config.getFilePath ()), file);
            if (path == null) {
                return JsonResponse.error ("上传失败！");
            }
            String url = "http://" + config.getFileHost () + ":" + config.getFilePort () + "/" + config.getFileUrl () + "/" + path;
            return JsonResponse.response (url, path);
        }
        return JsonResponse.error (ResultCodeEnum.TYPE_FALSE.getMessage ());

    }

    public JsonResponse uploadFile(MultipartFile[] files) {
//        List<String> path = new ArrayList<> ();
//        List<String> url = new ArrayList<> ();
        JSONArray path = new JSONArray ();
        JSONArray url = new JSONArray ();
        for (MultipartFile file : files) {
            if (checkType (getType (file))) {
                String paths = fileUtils.saveFile (Paths.get (config.getFilePath ()), file);
                path.add (paths);
                String urls = "http://" + config.getFileHost () + ":" + config.getFilePort () + "/" + config.getFileUrl () + "/" + paths;
                url.add (urls);
            }
        }
        if (url.size () == 0) {
            return JsonResponse.error (ResultCodeEnum.TYPE_FALSE.getMessage ());
        }

        return JsonResponse.response (url.toString (), path.toJSONString ());


    }


    /**
     * 判断该类型是否支持上传
     */
    private boolean checkType(String type) {
        if (falseType.size () == 0) {
            initType ();
        }
        return falseType.contains (type);
    }

    /**
     * 类型初始化
     */
    private void initType() {
        String[] strings = config.getFileType ().split (",");
        falseType.addAll (Arrays.asList (strings));
    }


    /**
     * 获取文件类型
     */
    private String getType(MultipartFile file) {
        String fileName = file.getOriginalFilename ();
        //获取文件类型
        assert fileName != null;
        return fileName.substring (fileName.lastIndexOf (".") + 1);
    }


    public JsonResponse path() {
        return JsonResponse.success ("http://" + config.getFileHost () + ":" + config.getFilePort () + "/" + config.getFileUrl () + "/");
    }

}
